package com.example.lending.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lending.Model.Book;
import com.example.lending.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.MyViewHolder> {
    private List<Book>books;
    private SharedPreferences sp;
    private Context context;
    public BookAdapter(List<Book>allBooks,Context context) {
        this.books=allBooks;
        this.context=context;
    }

    public BookAdapter() {
    }



    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
        View itemView;
        itemView=layoutInflater.inflate(R.layout.item_book,parent,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        final Book book = books.get(position);
        if(holder instanceof MyViewHolder){
            holder.tvName.setText("书籍名:"+book.getName());
            holder.tvSex.setText("价格："+book.getPrice());
            holder.tvTell.setText("数量："+book.getCount());
             holder.tvMajor.setText("类型："+book.getType());
            holder.tvClass.setText("浏览量："+book.getLook_number());
            if(book.getNumber()==1){
                holder.tvPublic.setText("图书已经被借阅");
            }else {
                holder.tvPublic.setText("图书还未被借阅");
            }

            holder.btn.setOnClickListener(new View.OnClickListener() { //跳转功能还没有写 可以在activity中用listener写
                @Override
                public void onClick(View v) {
                    sp=context.getSharedPreferences("adapter",context.MODE_PRIVATE);
                    sp.edit().putString("bookname",book.getName()).commit();//将这本书的名字传过去了
                    NavController controller= Navigation.findNavController(v);
                    controller.navigate(R.id.action_allBookFragment_to_updataBookFragment2);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(books==null){
            return 0;
        }
        System.out.println("0-------------size"+books.size());
        return books.size();
    }

    public void setBooks(List<Book> books){
        this.books = books;
    }
    public void setAllBooks(List<Book>allBooks){this.books=allBooks;}

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName,tvSex,tvMajor,tvClass,tvTell,tvPublic;
        Button btn;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName=itemView.findViewById(R.id.tv_namebook);
            tvClass=itemView.findViewById(R.id.tv_classbook);
            tvSex=itemView.findViewById(R.id.tv_sexbook);
            tvMajor=itemView.findViewById(R.id.tv_majorbook);
            tvPublic=itemView.findViewById(R.id.tv_publicbook);
            tvTell=itemView.findViewById(R.id.tv_telbook);
            btn=itemView.findViewById(R.id.btn_bianjibook);
        }
    }
}
